//
//  UIButton+JFAdd.m
//  JFWeChat
//
//  Created by jf on 2017/6/12.
//  Copyright © 2017年 JiFeng. All rights reserved.
//

#import "UIButton+JFAdd.h"

@implementation UIButton (JFAdd)

// 设置button的titleLabel和imageView的布局样式，及间距
- (void)jf_layoutButtonWithEdgeInsetsStyle:(JF_ButtonEdgeInsetsStyle)jf_style
                           imageTitleSpace:(CGFloat)jf_space {
    
//    [self sizeToFit];

    // 得到imageView和titleLabel的宽、高
    
    CGFloat jf_imageWidth  = 0.0;
    CGFloat jf_imageHeight = 0.0;
    
    CGFloat jf_labelWidth  = 0.0;
    CGFloat jf_labelHeight = 0.0;
    
    if (self.currentImage) {
        jf_imageWidth  = self.imageView.bounds.size.width;
        jf_imageHeight = self.imageView.bounds.size.height;
    }
    
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        jf_labelWidth  = self.titleLabel.intrinsicContentSize.width;
        jf_labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        jf_labelWidth  = self.titleLabel.frame.size.width;
        jf_labelHeight = self.titleLabel.frame.size.height;
    }
    
    UIEdgeInsets jf_imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets jf_labelEdgeInsets = UIEdgeInsetsZero;
    
    // 根据jf_style和jf_space得到jf_imageEdgeInsets和jf_labelEdgeInsets的值
    switch (jf_style) {
        case JF_ButtonEdgeInsetsStyleTop:
            jf_imageEdgeInsets = UIEdgeInsetsMake(-jf_labelHeight-jf_space/2.0, 0, 0, -jf_labelWidth);
            jf_labelEdgeInsets = UIEdgeInsetsMake(10, -20, -jf_imageHeight-jf_space/2.0, 0);
            break;
        case JF_ButtonEdgeInsetsStyleLeft:
            jf_imageEdgeInsets = UIEdgeInsetsMake(0, -jf_space/2.0, 0, jf_space/2.0);
            jf_labelEdgeInsets = UIEdgeInsetsMake(10, jf_space/2.0, 0, -jf_space/2.0);
            break;
        case JF_ButtonEdgeInsetsStyleBottom:
            jf_imageEdgeInsets = UIEdgeInsetsMake(0, 0, -jf_labelHeight-jf_space/2.0, -jf_labelWidth);
            jf_labelEdgeInsets = UIEdgeInsetsMake(-jf_imageHeight-jf_space/2.0, -jf_imageWidth, 0, 0);
            break;
        case JF_ButtonEdgeInsetsStyleRight:
            jf_imageEdgeInsets = UIEdgeInsetsMake(0, jf_labelWidth+jf_space/2.0, 0, -jf_labelWidth-jf_space/2.0);
            jf_labelEdgeInsets = UIEdgeInsetsMake(10, -jf_imageWidth-jf_space/2.0, 0, jf_imageWidth+jf_space/2.0);
            break;
        default:
            break;
    }
    self.titleEdgeInsets = jf_labelEdgeInsets;
    self.imageEdgeInsets = jf_imageEdgeInsets;
//    [self sizeToFit]; 
}

@end
